package Utils;

import java.io.FileReader;
import java.io.IOException;
import java.net.URL;
import java.sql.*;
import java.util.Properties;

/**
 * 该类为JDBC的工具类
 * 
 * 用于JDBC驱动的注册
 * 驱动的连接
 * 资源的释放
 *
 * 遗留问题：如何使用类加载的方式获取该项目下src下的文件
 *
 * @author Mc.Chai
 * @program: JavaWeb
 * @create 2021-12-14-15:50 15:50
 */
public class JDBCUtils {

    private static String url;
    private static String user;
    private static String password;
    private static String driver;
    //url,username,password只需要读取一次，不需要每次调用该类读取一次
    static{
        try {
            //创建集合类
            Properties properties = new Properties();
            /**
            *遗留问题：这里使用类加载的方式并不能获取module下的src文件
                //获取src目录下文件的方式  ClassLoader
                ClassLoader classLoader=JDBCUtils.class.getClassLoader();
                URL resource = classLoader.getResource("jdbc.properties");
                properties.load(new FileReader(resource.getPath()));
            */
            properties.load(new FileReader("day02_JDBC\\src\\jdbc.properties"));
            url = properties.getProperty("url");//注意:加双引号
            user=properties.getProperty("user");
            password=properties.getProperty("password");
            driver=properties.getProperty("driver");
            Class.forName(driver);
        } catch (IOException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }

    }
    /**
     * @Description: 获取数据库连接对象Connection
     * @Param: []
     * @return: java.sql.Connection
     * @Author: Mc.Chai
     * @Date: 2021/12/14
     * @Time: 
     */
    public static Connection getConnection(){
        Connection connection=null;
        try {
            connection= DriverManager.getConnection(url,user,password);
        }catch (SQLException throwables) {
            throwables.printStackTrace();
        }

        return connection;
    }
    /**
     * @Description: 对Statement和Connection资源的释放
     * @Param: [statement, connection]
     * @return: void
     * @Author: Mc.Chai
     * @Date: 2021/12/14
     * @Time: 
     */
    public static void close(Statement statement, Connection connection){
        if (statement!=null){
            try {
                statement.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }
        if (connection!=null){
            try {
                statement.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }
    }
    /**
     * @Description: 对ResultSet、Statement和Connection资源的释放
     * @Param: [resultSet, statement, connection]
     * @return: void
     * @Author: Mc.Chai
     * @Date: 2021/12/14
     * @Time: 
     */
    public static void close(ResultSet resultSet, Statement statement, Connection connection){
        if (resultSet!=null){
            try {
                resultSet.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }
        if (statement!=null){
            try {
                statement.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }
        if (connection!=null){
            try {
                statement.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }
    }
}
